//
//  AdministratorListViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/8/18.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AdministratorListViewController.h"
#import "FocusAFansTableViewCell.h"
#import "User.h"

@interface AdministratorListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *administratorArr;

@end

@implementation AdministratorListViewController {
    UITableView     *_tableView;
    FocusAFansTableViewCell *_cell;
    UILabel         *_headL;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UISeparatedColorRGB;
    
    [self GETadministrator];
    
    [self buildUI];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    self.navigationItem.title = @"管理员列表";
    

    
    _headL = [[UILabel alloc] init];
    [self.view addSubview:_headL];
    _headL.font = kUHSystemFontWithSize(15);
    _headL.textColor = kBlackColor;
    _headL.layer.borderColor = UISeparatedColorRGB.CGColor;
    _headL.text = [NSString stringWithFormat:@"    当前管理员:(1/5)"];
    _headL.frame = CGRectMake(0, 64, SCREEN_WIDTH, 40);
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, SCREEN_WIDTH, 0) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"FocusAFansTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}

#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    User *model = self.administratorArr[indexPath.row];
    
    _cell.model = model;
    
    [_cell.optionsBtn setTitle:@"删除" forState:UIControlStateNormal];
    _cell.optionsBtn.tag = indexPath.row;
    [_cell.optionsBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.administratorArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SW(7)+20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
}

#pragma mark ==========响应方法==========
- (void)deleteAction:(UIButton *)btn {
    
    // DELETE /users/{anchor_id}/lives/admins/{user_id} 删除主播管理员 anchor_id *主播ID user_id *管理员ID
    
    NSString *user_id = [self.administratorArr[btn.tag] valueForKey:@"user_id"];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/lives/admins/%@", APIUser, self.hostId, user_id];
    [[NetworkTool sharedTool] requestWithName:@"删除主播管理员" URLString:url parameters:nil method:@"DELETE" callBack:^(id responseObject) {
        
        [self.administratorArr removeObjectAtIndex:btn.tag];
        
        _tableView.frame = CGRectMake(0, 104, SCREEN_WIDTH, (SW(7)+20)*self.administratorArr.count);
        _headL.text = [NSString stringWithFormat:@"    当前管理员:(%ld/5)", self.administratorArr.count];
        
        [_tableView reloadData];
        
        
        
        // 发送解除管理员消息通知
        NSNotification *notification = [NSNotification notificationWithName:@"sendDeleteAdministratorMsg" object:nil userInfo:@{@"id":user_id}];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
        
    }];
    
    

    
    
//    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:btn.tag inSection:0];  //你需要更新的组数中的cell
//    [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationMiddle]; //collection 相同
    
}
/**
 获取该主播的管理员
 */
- (void)GETadministrator {
    
    // GET /users/{id}/lives/admins 获取该主播的管理员 id *主播ID
    NSString *url = [NSString stringWithFormat:@"%@/%@/lives/admins", APIUser, self.hostId];
    [[NetworkTool sharedTool] requestWithName:@"获取该主播的管理员" URLString:url parameters:nil method:@"GET" callBack:^(id responseObject) {
        
        for (NSDictionary *dict in responseObject[@"data"]) {
            
            User *model = [[User alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.administratorArr addObject:model];
            
        }
        
        _tableView.frame = CGRectMake(0, 104, SCREEN_WIDTH, (SW(7)+20)*self.administratorArr.count);
        _headL.text = [NSString stringWithFormat:@"    当前管理员:(%ld/5)", self.administratorArr.count];
        [_tableView reloadData];
        
    }];
}
// 懒加载
- (NSMutableArray *)administratorArr {
    if (_administratorArr == nil) {
        _administratorArr = [[NSMutableArray alloc] init];
    }
    return _administratorArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
